<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="loadListData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表展示开始-->
    <el-table :data="listData" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="公司名称" width="120" >
      </el-table-column>
      <el-table-column prop="tel" label="电话" width="100" >
      </el-table-column>
      <el-table-column prop="registerTime" label="注册时间" width="120" >
      </el-table-column>
      <el-table-column prop="address" label="地址" min-width="180" >
      </el-table-column>
      <el-table-column prop="logo" label="图标" min-width="180" >
        <template scope="scop">
            <img :src="'http://47.95.117.210:22122'+scop.row.logo" width="100" height="100">
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100" :formatter="formatSex" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleSee(scope.$index, scope.row)">查看</el-button>
          <el-button type="primary" size="small" @click="handlEexamine(scope.$index, scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--列表展示结束-->

    <!--审核弹出框开始-->
    <el-dialog title="审核店铺" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="问题类型" :label-width="formLabelWidth" >
          <el-select v-model="form.problem" placeholder="请选择问题类型" clearable>
            <el-option label="账户问题" value="shanghai"></el-option>
            <el-option label="logo问题" value="beijing"></el-option>
            <el-option label="地址类" value="beijing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.remarks" autocomplete="off"></el-input>
          <!--<el-input v-model="form.name" autocomplete="off"></el-input>-->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button @click="dialogFormVisible = false" @click.native="reiect">驳 回</el-button>
        <el-button type="primary" @click="dialogFormVisible = false" @click.native="modifyState">通 过</el-button>
      </div>
    </el-dialog>
    <!--审核弹出框结束-->

    <!--批量删除-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="total, sizes,prev, pager, next"
                     @current-change="handleCurrentChange"
                     @size-change="handleSizeChange"
                     :page-sizes="[5, 10, 20, 30]"
                     :page-size="pageSize"
                     :total="total" style="float:right;">
      </el-pagination>
    </el-col>
    <!---->

    <!--新增界面-->
    <el-dialog :title="title" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" >
          <el-input v-model="addForm.tel" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="注册时间" >
          <!--<el-input v-model="addForm.registerTime" auto-complete="off"></el-input>-->
          <span class="demonstration"></span>
          <el-date-picker
              v-model="addForm.registerTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地址" >
          <el-input v-model="addForm.address" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标" >
          <img :src="'http://47.95.117.210:22122'+addForm.logo" width="100" height="100">
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="addForm.state">
            <el-radio class="radio" :label="1">开启</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
    <!--新增界面结束-->

  </section>
</template>

<script>

import {addUser, batchRemoveUser, removeUser} from "../../api/api";
import util from "../../common/js/util";

export default {
  data() {
    return {
      formLabelWidth: '120px',//审核框大小
      form:{
        problem:'',
        remarks:'',
      },//审核框数据
      dialogFormVisible:false,//审核框默认关闭
      page:1,//默认是第一页
      pageSize:5,//默认展示十条
      sels: [],//列表选中列,多选选中的id数组
      title:'',
      addFormVisible: false,//新增界面是否显示
      //新增界面需要展示的数据
      addForm: {

      },
      //用户必填
      addFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      filters: {
        name: ''
      },
      listData:[],
      listLoading:false,
      addLoading: false,//加载动画
      total:0,
      shop:{}
    }
  },


  methods: {

    //修改
    handleSee (index, row) {
      //回显数据
      this.addForm = Object.assign({}, row);
      console.debug(this.addForm)
      //在新增的时候把parts数组进行了改动为对象，但是回显的时候需要数组,
      //this.addForm.parts=7根据这个测试得到，我们只需要拿到一个上级id就可以了
      /*this.addForm.parts=row.parent_id*/
      //弹出模态框
      this.addFormVisible=true
      //给title定义
      this.title="查看"

    },
    reiect(){
      this.addLoading = true;
      this.$http.put("/shop/upt",this.shop).then(res =>{
        let {success,message} = res.data
        if (success){
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '操作成功',
            type: 'success'
          });
        }else {
          this.$message('错了哦，这是一条错误消息');
        }
        this.addLoading = false;
        //刷新数据
        this.loadListData()
      })
    },

    modifyState(shop){
      this.addLoading = true;
      this.$http.put("/shop/upt",this.shop).then(res =>{
        let {success,message} = res.data
        if (success){
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '操作成功',
            type: 'success'
          });
        }else {
          this.$message('错了哦，这是一条错误消息');
        }
        this.addLoading = false;
        //刷新数据
        this.loadListData()
      })
    },

    //弹出审核框
    handlEexamine(index, row){
      this.shop=row
      console.debug(this.shop)
      this.form={
        problem:'',
        region:'',
      }
        this.dialogFormVisible=true;
    },

    //当点击每页展示条数时
    handleSizeChange(val){
      this.pageSize = val
      this.loadListData()
    },

    handleCurrentChange(val) {
      this.page = val;
      this.loadListData();
    },

    //批量删除
    batchRemove () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/shop",ids).then(res =>{
          let {success,message} = res.data
          if (success){
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            this.$message.error('错了哦，这是一条错误消息');
          }
          //刷新数据
          this.loadListData()
        })
      }).catch(() => {

      });
    },

    //新增
    addSubmit () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            this.$http.put("/shop",this.addForm).then(res =>{
              let {success,message} = res.data
              if (success){
                this.listLoading = false;
                //NProgress.done();
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
              }else {
                this.$message('错了哦，这是一条错误消息');
              }
              this.addLoading = false;
              this.addFormVisible = false;
              //刷新数据
              this.loadListData()
            })
          });
        }
      });
    },

    //显示新增界面
    handleAdd: function () {
      this.title="新增"
      this.addFormVisible = true;
      this.addForm = {
        name: '',
        tel:'',
        registerTime:'',
        state: 0,
        logo:'',
        admin_id:'',
        address: ''
      };
    },

    //删除
    handleDel (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/shop"+"/"+row.id).then(res =>{
          let {success,message} = res.data
          if (success){
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            this.$message.error('错了哦，这是一条错误消息');
          }
          //刷新数据
          this.loadListData()
        })
      }).catch(() => {

      });
    },

    //显示编辑界面
    handleEdit (index, row) {
      console.debug(2222)
      console.debug(row)
      //回显数据
      this.addForm = Object.assign({}, row);
      //弹出模态框
      this.addFormVisible=true
      //给title定义
      this.title="修改"
    },

    formatSex (row, column) {
      return row.state == 1 ? '开启' : row.state == 0 ? '禁用' : '待审核';
    },

    selsChange (sels) {
      this.sels = sels;
    },


    loadListData(){
      let param = {
        currentPage:this.page,
        pageSize:this.pageSize,
        keywords:this.filters.name
      }

      this.$http.post("/shop",param).then(res =>{
        this.listData=res.data.data;
        this.total=res.data.totals
        this.listLoading=false

      })
    },


  },

  mounted() {
    this.loadListData()
  }

}

</script>

<style scoped>

</style>
